﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mNC.Common
{
    public class EmailHelper
    {
        public static void SendMailUseGmail(string strAdress, string[] strCC, string strHtml, DataTable data)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

            //foreach (string str in strAdress)
            //{
            msg.To.Add(strAdress);
            //}
            if (strCC != null)
            {
                foreach (string str in strCC)
                {
                    msg.CC.Add(str);
                }
            }
            //抄送人增加自己
            msg.CC.Add(Lamex.Common.Loginer.CurrentUser.Email);

            msg.From = new MailAddress(Lamex.Common.Loginer.CurrentUser.Email, "", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/


            msg.Subject = "NC转码问题跟进";//邮件标题    
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码    
            // msg.Body = "Dear ALL  良率报表详见附件，请查收！";//邮件内容    
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
            msg.IsBodyHtml = true;//是否是HTML邮件    
            msg.Priority = MailPriority.Normal;//邮件优先级    

            DataView dataView = data.DefaultView;
            data = dataView.ToTable(true, new string[] { "ISSUE_NO", "ISSUE_FROM", "SUBMITTER", "SUBMIT_TIME", "PART_OR_DWG_NO", "ISSUE_CATEGORY", "ISSUE_DESC", "MO_PART_QTY", "RESPONSIBLE_DEPT", "PERSON_IN_CHARGE" });
            //设置中文列名
            data.Columns["ISSUE_NO"].ColumnName = "问题编号";
            data.Columns["ISSUE_FROM"].ColumnName = "问题来源";
            data.Columns["SUBMITTER"].ColumnName = "提交人";
            data.Columns["SUBMIT_TIME"].ColumnName = "提交时间";
            data.Columns["PART_OR_DWG_NO"].ColumnName = "部件号或图纸号";
            data.Columns["ISSUE_CATEGORY"].ColumnName = "问题类别";
            data.Columns["ISSUE_DESC"].ColumnName = "问题描述";
            data.Columns["MO_PART_QTY"].ColumnName = "问题数量";
            data.Columns["RESPONSIBLE_DEPT"].ColumnName = "跟进部门";
            data.Columns["PERSON_IN_CHARGE"].ColumnName = "跟进人";



            string MailBody = "Dear,";
            MailBody += "<br>";
            MailBody += "&nbsp;&nbsp;&nbsp以下" + data.Rows[0]["问题来源"].ToString().Trim() + "相关事项，请跟进并回复，谢谢！<br>";
            MailBody += "<br>";
            MailBody += "<table cellspacing=\"1\" cellpadding=\"3\" border=\"1\"  style=\"font-size: 10pt;line-height: 15px;\">";
            MailBody += "<div align=\"center\">";
            MailBody += "<tr>";
            //处理表头格式
            for (int hcol = 0; hcol < data.Columns.Count; hcol++)
            {
                MailBody += "<td bgcolor=\"436EEE\" style=\"font-size: 13pt\">";
                MailBody += data.Columns[hcol].ColumnName;
                MailBody += "</td>";
            }
            MailBody += "</tr>";

            //处理表格内容
            for (int row = 0; row < data.Rows.Count; row++)
            {
                MailBody += "<tr>";
                for (int col = 0; col < data.Columns.Count; col++)
                {
                    MailBody += "<td bgcolor=\"FFFFFF\">";
                    if (data.Columns[col].ColumnName.ToString() == "问题编号")
                    {
                        //string strHtml = @"\\Tx-fs10.lamex.com\tx-fs1\IMA\MES\ShareResource\NC_Issue_Image\20200311134000487.jpg";
                        MailBody += "<a href='" + strHtml + "'> " + data.Rows[row][col].ToString() + " </a> ";
                    }
                    else
                        MailBody += data.Rows[row][col].ToString();
                    MailBody += "</td>";
                }
                MailBody += "</tr>";
            }
            MailBody += "</table>";
            MailBody += "</div>";
            //增加署名
            MailBody += "<br>";
            MailBody += "Best Regards,";
            MailBody += "<br>";
            MailBody += Lamex.Common.Loginer.CurrentUser.AccountName;
            MailBody += "<br>";
            MailBody += "联系电话：" + Lamex.Common.Loginer.CurrentUser.Tel;
            msg.Body = MailBody;



            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("mailsender@lamex.com.hk", "Bl@7701167//");
            //client.Credentials = new System.Net.NetworkCredential("Bill.Luo@lamex.com.hk", "");
            client.Host = "172.23.134.118";
            // client.EnableSsl = true;//经过ssl加密    
            object userState = msg;
            try
            {
                client.Send(msg);
                GC.Collect();

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错");
            }
        }

    }
}
